#include <iostream>
//中介者模式

class House; //房子
//抽象中介者
class Mediator
{
public:
    void SetHouse(House* sell, House* buy)
    {
        h1 = sell;
        h2 = buy;
    }
virtual void SendMsg(const std::string& msg,House *h)= 0;

protected:
    House *h1;
    House *h2;
};

class House //房子
{
public:
    House(Mediator* m):m(m){}
    virtual void SendMsg(const std::string& name) = 0;//发消息
    virtual void RecvMsg(const std::string& name) = 0;//收消息

protected:
Mediator *m;//中介
};

class SelHouse:public House //卖房子的人
{
public: 
    //继承构造
    using House::House;
    void SendMsg(const std::string& msg)
    {
        m->SendMsg(msg, this);
    }
    void RecvMsg(const std::string& msg)
    {
        std::cout<<"买房子的人收到消息："<<msg<<std::endl;
    }
};

class BuyHouse:public House //卖房子的人
{
public: 
    //继承构造
    using House::House;
    void SendMsg(const std::string& msg)
    {
        m->SendMsg(msg, this);
    }
    void RecvMsg(const std::string& msg)
    {
        std::cout<<"卖房子的人收到消息："<<msg<<std::endl;
    }
};

class ConcreteMediator:public Mediator
{
public:
    void SendMsg(const std::string& msg, House *h)
    {
        if(h == h1)
        {
            h2->RecvMsg(msg);
        }
        else
        {
            h1->RecvMsg(msg);
        }
    }
};

int main()
{
    Mediator *m = new ConcreteMediator;
    House *h1 = new SelHouse(m);//卖房子的人
    House *h2 = new BuyHouse(m);//买房子的人

    m->SetHouse(h1, h2);

    h2->SendMsg("200买不买");
    h1->SendMsg("不买,至少300");
    return 0;
}
